package com.xuxiangyang.competitionsystem.controller;

import com.xuxiangyang.competitionsystem.entities.Artical;
import com.xuxiangyang.competitionsystem.entities.Competition;
import com.xuxiangyang.competitionsystem.entities.RetResponse;
import com.xuxiangyang.competitionsystem.entities.RetResult;
import com.xuxiangyang.competitionsystem.service.IArticalService;
import com.xuxiangyang.competitionsystem.service.ICompService;
import com.xuxiangyang.competitionsystem.tools.UUIDTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/artical")
@CrossOrigin
public class ArticalController {

    @Autowired
    private IArticalService articalService;

    @Autowired
    private ICompService compService;

    @Autowired
    private UUIDTools tools;

    //新增
    @RequestMapping("/add")
    public RetResult add(Artical artical) {
        artical.setArticalId(tools.getUUID());
        articalService.insert(artical);
        return RetResponse.makeOKRsp();
    }

    //获取所有
    @RequestMapping("/getListAll")
    public RetResult getListAll() {
        List<Artical> articalList =  articalService.getListAll();
        return RetResponse.makeOKRsp(articalList);
    }

    //获取所有
    @RequestMapping("/getListByType")
    public RetResult getListByType(Integer id) {
        List<Competition> compList = compService.getAllComp();
        List<Artical> newsList =  articalService.getListByNews();
        List<Artical> noticesList =  articalService.getListByNotice();
        List<Artical> downloadsList =  articalService.getListByDownload();
        Map<String,List> map = new HashMap<>();
        if(newsList.size() > 5) {
            newsList = newsList.subList(0,5);
        }if(noticesList.size() > 5) {
            noticesList = noticesList.subList(0,5);
        }if(downloadsList.size() > 5) {
            downloadsList = downloadsList.subList(0,5);
        }
        map.put("newsList",newsList);
        map.put("noticesList",noticesList);
        map.put("downloadsList",downloadsList);
        map.put("compList",compList);
        return RetResponse.makeOKRsp(map);
    }

    //编辑文章
    @RequestMapping("/edit")
    public RetResult edit(Artical artical) {
        articalService.updateArtical(artical);
        return RetResponse.makeOKRsp();
    }

    //删除文章
    @RequestMapping("/delete")
    public RetResult delete(Artical artical) {
        articalService.deleteById(artical);
        return RetResponse.makeOKRsp();
    }
}
